<?php 
/**
 * 
 */
 class CustomerController extends AuthController
 {
 	
 	private $_db;
	public function __init(){
		parent::__init();
		$this->_db=K('Customer');
	}

	//客户列表
 	public function index()
 	{
 		$page = new Page($this->_db->count(),20);
		$this->page =$page->show();
		$this->customer=$this->_db->limit($page->limit())->order('id desc')->all();
		$this->display();
 	}

 	//添加客户
	Public function add(){
		if(IS_POST){
			if($this->_db->addCustomer()){
				$this->success('客户添加成功','index');
			}else{
				$this->error($this->_db->error);
			}
		}else{
			$this->display();
		}
	}

	//修改客户
	Public function edit(){
		if(IS_POST){
			if($this->_db->editCustomer()){
				$this->success('客户修改成功','index');
			}else{
				$this->error($this->_db->error);
			}
		}else{
			$id = Q('get.id',0,'intval');
			$this->customer = $this->_db->find($id);
			$this->display();
		}
	}
	//删除客户
	Public function del(){
		$id=Q("id");
		if(empty($id)){
			$this->error('参数传递错误');
		}else{
			if(is_array($id)){
				foreach($id as $id){
					$this->_db->del($id);
				}
				$this->success('客户信息删除成功','index');
			}else{
				$this->_db->del($id);
				$this->success('客户信息删除成功','index');
			}
		}
		
	}

	//检查客户是否已存
	Public function ajaxCheck(){
		$name=Q('name');
		if($this->_db->find(array('name'=>$name))){
			$this->error($name.'已经存在');
		}else{
			$this->success('验证通过');
		}
		
	}
 } ?>